/**
 * Created by willem.
 */
import _ from 'lodash';
import {render as reactDomRender, unmountComponentAtNode } from 'react-dom';
import React from 'react';
import deepClone from "./deepClone";
import {ConfigProvider} from "@/components";
import moment from "moment";
import styles from "@/pages/dv/DvEditor/BoxContainer/index.module.scss";
// import {childConfig} from '@/config';

interface IHelper {
  deepClone: any;
  renderModal: (component: any, container: any) => any
}

class Helper implements IHelper{
  deepClone = deepClone;

  debounceTimer: any = null;

  /**
   * 渲染弹出窗Modal
   * @param component
   * @param isSingle 是否单一弹窗
   */
  renderModal(component: any, isSingle = true) {

    const $root = document.body;
    if(isSingle){
      // 单一弹窗
      const containerId = 'wocloud-single-modal-box'; // 单一弹窗容器盒子
      let container = document.querySelector('#' + containerId);
      if (!container) {
        // 不存在容器时
        const initContainer = document.createElement('div');
        initContainer.setAttribute('id', containerId);
        initContainer.style.height = '0px';
        $root.appendChild(initContainer);
        container = initContainer;
      } else {
        // 清空 容器
        unmountComponentAtNode(container);
      }
      // console.log(component, container);

      return reactDomRender(<ConfigProvider>
        {component}
      </ConfigProvider>, container); // 渲染到容器中
    } else {
      // 可层叠弹窗
      const containerId = 'wocloud-multi-modal-box'; // 层叠弹窗容器盒子
      // 不存在容器时
      const container = document.createElement('div');
      container.setAttribute('class', containerId);
      container.style.height = '0px';
      $root.appendChild(container);

      return reactDomRender(<ConfigProvider>
        {component}
      </ConfigProvider>, container); // 渲染到容器中
    }
  }

  /**
   * 添加隐藏dom
   * @param domElement
   * @param domId
   */
  addHiddenDom(domElement: any, domId: string) {
    const oldDomElement = document.querySelector('#' + domId);
    if (domId && oldDomElement) {
      unmountComponentAtNode(oldDomElement);
      document.body.removeChild(oldDomElement);
    }
    const newDomElement = document.createElement('div');
    newDomElement.style.display = 'none';
    newDomElement.appendChild(domElement);
    document.body.appendChild(newDomElement);
  }

  /**
   * 跳转页面
   * @param url
   * @param timeout
   */
  redirect(url: string, timeout?: number) {
    setTimeout(function () {
      location.href = url || location.href;
    }, timeout || 0);
  }


  /**
   * 浮点型保留小数
   * @param {Number} num
   * @param {Number} fixNum
   * @param {Mixed} defaultVal 格式化错误的默认值
   * @return {string}
   */
  toFixed(num: string, fixNum = 2, defaultVal = '-') {
    let result = Number(num).toFixed(fixNum);
    return _.isNaN(result) || result === 'NaN' ? defaultVal : result;
  }

  /**
   * 是否是一个真实的数值字符串
   * @param value
   */
  isRealNumeric(value: string): boolean {
    return /^(\d+\.)?\d+$/.test(value);
  }

  /**
   * 时间格式化
   * @param {Date|number|Moment} date
   * @param {string} template
   * @return {string}
   */
  dateFormat(date = _.now(), template = 'YYYY-MM-DD HH:mm:ss') {
    if (this.isRealNumeric(date)) {
      date = parseInt(date);
    }

    return moment(date).format(template);
  }

  /**
   * 防抖函数
   * @param {Function} fn     回调函数
   * @param {Number} delay    延迟事件
   * @returns {Function}
   */
  debounce(fn: any, delay: number) {

    const self = this

    return function (...args: any[]) {
      self.debounceTimer && clearTimeout(self.debounceTimer);
      const context = this;
      self.debounceTimer = setTimeout(function () {
        fn.apply(context, args);
      }, delay);

    };
  }


  /**
   * 获取queryString
   *
   * @returns {Object}
   */
  getUrlParams() {
    let urlParams = {};
    const queryString = decodeURIComponent(window.location.search.substr(1));
    if (queryString) {
      _.each(queryString.split('&'), (paramString) => {
        const param = paramString.split('=');
        urlParams[param[0]] = param[1];
      });
    }

    return urlParams;
  }

  /**
   * 获取URL参数
   *
   * @param {string} name
   * @returns {string}
   */
  getUrlParam(name) {
    return this.getUrlParams()[name];
  }
}


export default new Helper();

